Vue.http.options.emulateJSON = true;
const vm = new Vue({
    el: '#dv',
    data: {
        showStudent: {
            size: 5,
            page: 1,
        },
        id: '',
        student_id: '',
        course_id: '',
        upStuId: '',
        upCorId: '',
        flag: false,
        total: 0,
        list: [],
        studentId:[],
        courseId:[]
    },
    methods: {
        getAllList(mes) {
            this.$http.post('../student_course_ref/list.php',
                mes)
                .then(result => {
                    let data = result.body.data
                    this.total = data.count
                    if (result.status === 200) {
                        this.list = data.list
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        getStudentsId(){
            this.$http.post('../student/list.php',
                {size:999, page:1})
                .then(result => {
                    if (result.status === 200) {
                        let data = result.body.data
                        this.studentId = data.list
                    }else {
                        alert('服务器出错')
                    }
                })
        },
        getCourseId(){
            this.$http.post('../course/list.php',
                {size:999, page:1})
                .then(result => {
                    if (result.status === 200) {
                        let data = result.body.data
                        this.courseId = data.list
                    }else {
                        alert('服务器出错')
                    }
                })
        },
        add() {
            this.$http.post('../student_course_ref/add.php',
                {student_id: this.student_id, course_id: this.course_id})
                .then(result => {
                    if (result.status === 200) {
                        this.getAllList(this.showStudent)
                    } else {
                        alert('服务器出错')
                    }
                })
            this.student_id = this.course_id = ''
        },
        update() {
            this.$http.post('../student_course_ref/update.php',
                {id: this.id, student_id: this.upStuId, course_id: this.upCorId})
                .then(result => {
                    if (result.status === 200) {
                        this.flag = false
                        this.getAllList(this.showStudent)
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        del(id) {
            this.$http.post('../student_course_ref/delete.php',
                {id: id})
                .then(result => {
                    if (result.status === 200) {
                        this.getAllList(this.showStudent)
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        choosePage(index) {
            this.showStudent.page = index
            this.getAllList(this.showStudent)
        },
        modify(id) {
            this.flag = true
            this.id = id
            this.$http.post('../student_course_ref/get.php',
                {id: id})
                .then(result => {
                    if (result.status === 200) {
                        let data = result.body.data
                        this.upStuId = data.student_id
                        this.upCorId = data.course_id
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        next() {
            this.showStudent.page += 1
            if (this.showStudent.page > Math.ceil(this.total / this.showStudent.size)) {
                return
            }
            this.getAllList(this.showStudent)
        },
        prev() {
            this.showStudent.page -= 1
            if (this.showStudent.page < 1) {
                return
            }
            this.getAllList(this.showStudent)
        }
    },
    created() {
        this.getStudentsId()
        this.getCourseId()
        this.getAllList(this.showStudent)
    }
})